package com.dodol.util;

import com.dodol.common.BaseTestCase;
import com.dodol.common.TestStep;
import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 80736
 * @Date: 2018/9/3 13:53
 * @Description:
 */
public class ObjectUtil {

    /**
     * 转换测试用例列表为对象列表
     * 为了在测试报告中显示步骤
     *
     * @param testCases
     * @return
     */
    public static Object[][] convertObject(List<BaseTestCase> testCases) {
        List<Object[]> objList = new ArrayList<Object[]>();
        for (BaseTestCase testCase : testCases) {
            Object[] obj = new Object[1];
            obj[0] = testCase;
            objList.add(obj);
        }

        Object[][] objects = new Object[objList.size()][1];
        for (int i = 0; i < objList.size(); i++) {
            Object[] ob = objList.get(i);
            for (int j = 0; j < ob.length; j++) {
                objects[i][j] = ob[j];
            }
        }
        return objects;
    }

    /**
     * 修复测试用例对象内driver为空的情况，把当前driver传入verification
     *
     * @param testCase
     * @param driver
     * @return
     */
    public static BaseTestCase repairBaseTestCaseObject(BaseTestCase testCase, WebDriver driver) {
        List<TestStep> steps = testCase.getStep();
        for (TestStep step : steps) {
            if (step.getVerification() != null) {
                WebDriver driver1 = step.getVerification().getDriver();
                if (driver1 == null) {
                    step.getVerification().setDriver(driver);
                }
            }
        }
        testCase.setSteps(steps);
        return testCase;
    }
}
